package com.centling.smartSealForPhone.activity

import android.content.Intent
import android.os.Bundle
import android.view.View
import com.centling.smartSealForPhone.R
import com.centling.smartSealForPhone.constants.GlobalData
import com.centling.smartSealForPhone.constants.UrlConstants
import com.centling.smartSealForPhone.utils.GlideApp
import com.centling.smartSealForPhone.utils.OkHttpUtil
import com.centling.smartSealForPhone.utils.ShowToast
import kotlinx.android.synthetic.main.activity_auditor_main.*
import org.json.JSONObject
import java.util.*

/**
 * Created by Jessie on 2016/9/7.
 */
class AuditorMainActivity : TitleBarActivity() {

    private fun sendRequestToFetchUnread() {
        val param = HashMap<String, String>()
        postJsonEnqueue(UrlConstants.GET_AUDITOR_SEAL_TIME, param, object : OkHttpUtil.NetCallBack {
            override fun onSucceed(json: String) {
                val jsonObject = JSONObject(json)
                val check = jsonObject.optString("checkCount")
                val review = jsonObject.optString("recheckCount")

                if (check == "0") {
                    tv_audotor_fist_num.visibility = View.INVISIBLE
                } else {
                    tv_audotor_fist_num.visibility = View.VISIBLE
                    tv_audotor_fist_num.text = check
                }

                if(review == "0"){
                    auditor_review_num.visibility = View.GONE
                }else{
                    auditor_review_num.text = review
                    auditor_review_num.visibility = View.VISIBLE
                }
            }
            override fun onFailed(reason: String) {
            }
        })
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setTitleBarVisibility(View.GONE)
        setContentView(R.layout.activity_auditor_main)

        GlideApp.with(mContext).load(GlobalData.appImageMainAdmin).placeholder(R.drawable.iv_auditor_banner).into(iv_auditor_home_banner);
        ll_auditor_first_instance_main.setOnClickListener {
            startActivity(Intent(mContext, AuditorSearchResultActivity::class.java)
                    .putExtra("applicantUserName", "").putExtra("sealName", "")
                    .putExtra("status", "0").putExtra("isValid", "0").putExtra("applyType", "")
                    .putExtra("applyReasonType", "").putExtra("applyTimeStart", "").putExtra("applyTimeEnd", "")
                    .putExtra("authTimeStart", "").putExtra("authTimeEnd", "").putExtra("reviewTimeStart", "")
                    .putExtra("reviewTimeEnd", "").putExtra("isFirst", "0"))
        }
        rl_auditor_review_main.setOnClickListener {
            startActivity(Intent(mContext, AuditorSearchResultActivity::class.java)
                    .putExtra("applicantUserName", "").putExtra("sealName", "").putExtra("status", "58").putExtra("isValid", "0")
                    .putExtra("applyType", "COUNT_LIMIT").putExtra("applyReasonType", "").putExtra("applyTimeStart", "")
                    .putExtra("applyTimeEnd", "").putExtra("authTimeStart", "").putExtra("authTimeEnd", "")
                    .putExtra("reviewTimeStart", "").putExtra("reviewTimeEnd", "").putExtra("isFirst", "1"))
        }
        rl_auditor_history_main.setOnClickListener { startActivity(Intent(mContext, AuditorSealHistoryActivity::class.java)) }
        ll_auditor_setting_main.setOnClickListener { startActivity(Intent(mContext, AuditorSettingActivity::class.java)) }
    }

    override fun onResume() {
        super.onResume()
        sendRequestToFetchUnread()
    }

    private var exitTime: Long = 0

    override fun onBackPressed() {
        if (System.currentTimeMillis() - exitTime > 2000) {
            ShowToast.shortTime("再按一次退出程序")
            exitTime = System.currentTimeMillis()
        } else {
            super.onBackPressed()
        }
    }
}
